export default {
  data() {
    return {
      edit: 0,
      form: {
        'invoice_kind': 0, //发票类型 0：个人/非事业单位 1：企业
        'person_title': '', //抬头名称
        'invoice_type': 2, //发票种类 2：增值税专票 3：增值税普票
        'company_title': '', //公司抬头
        'tax_number': '', //公司税号
        'bank_address': '', //企业开户行
        'card_numbers': '', //银行卡号
        'registered_telephone': '', //注册电话
        'registered_address': '' //注册地址
      },
      typeActive: 2,
      radioList: [{
        name: '单位',
        val: 1,
        disabled: false
      },
      {
        name: '个人/非企事业单位',
        val: 0,
        disabled: false
      }
      ],
      radio: 0,
      switchVal: false
    }
  },
  onLoad(options) {
    if (options.id) {
      let data = JSON.parse(decodeURIComponent(options.id))
      console.log(data)
      if (data) {
        this.edit = 1
        uni.setNavigationBarTitle({
          title: '编辑抬头'
        })
        this.form = data
      }
    }
  },
  methods: {
    changeRadio(e) {
      // console.log(e)
      this.typeActive = 2
      this.radio = e
    },
    changeTypeActive(index) {
      // console.log(index)
      this.typeActive = index

    },
    async submit() {
      let status = false
      console.log(this.$refs.uForm.model)
      if (this.form.invoice_kind === 0) {
        if (this.form.person_title) {
          status = true
        } else {
          status = false
          uni.showToast({
            title: '请填写抬头名称',
            icon: 'none'
          })

        }
      } else {
        for (var i in this.form) {
          console.log(i)
          if (i !== 'person_title') {
            if (this.form[i]) {
              status = true
            } else {
              status = false
              uni.showToast({
                title: '请填写抬头信息',
                icon: 'none'
              })
            }
          }
        }
      }
      console.log(status)
      if (status) {
        try {
          var res = {}
          if (this.edit === 0) {
            res = await this.$api.common.AddReceiverAddress(this.form)
          } else {
            res = await this.$api.common.UpdateInvoiceTitle(this.form)
          }

          console.log(res)
          uni.showToast({
            title: res.data,
          })
          setTimeout(() => {
            uni.navigateBack(-1)
          }, 1500)

        } catch (e) {
          uni.showToast({
            title: this.edit===0?'新增失败，请稍后重试！':'修改失败，请稍后重试！',
            icon: 'none'
          })
          //TODO handle the exception
        }
      }
    },
    // 删除抬头
    async DeleteInvoiceTitle() {
      try{
        const res = await this.$api.common.DeleteInvoiceTitle({
          id: this.form.id
        })
        uni.showToast({
          title: res.data,
        })
        setTimeout(() => {
          uni.navigateBack(-1)
        }, 1500)

      }catch(e){
        uni.showToast({
          title: '删除失败，请稍后重试！',
          icon: 'none'
        })
      }

    }
  }
}
